function [eye_delta_fb,eye_delta_pb,eye_fb_frames,eye_pb_frames]=plot_pert_vs_eye(aux_data,nbr_frames,pupil_pos,iframe_times,draw_fig)
ids=[-1:-1:-6 1:7];
l_win=1500;
r_win=2500;
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
stim_id=round(aux_data(3,:)*2);
shutterOpen=find(abs(diff(aux_data(1,:)>1))==1);

if length(nbr_frames)>5
    order=[];
    for xnd=1:length(nbr_frames)
        if nbr_frames(xnd)==20000
            if xnd==length(nbr_frames)
                order(xnd)=3;
            else
                order(xnd)=1;
            end
        else
            order(xnd)=2;
        end
    end
else
    order=[1 2 1 2 3];
end
a=1:length(order);
order(max(a(order==2))+1:end)=3;
nbr_exp=length(order);
nbr_bas=sum(order==3);
bas_exp=[];
for lnd=1:length(nbr_bas)
    bas_exp(lnd)=shutterOpen((nbr_exp*2)-((lnd*2)-1));
end
bas_exp=min(bas_exp);

delta=[];
for ind=1:size(pupil_pos,2)-1
    dx=abs(pupil_pos(1,ind)-pupil_pos(1,ind+1));
    dy=abs(pupil_pos(2,ind)-pupil_pos(2,ind+1));
    delta(ind)=sqrt(dx^2+dy^2);
    if ind==size(pupil_pos,2)-1
        delta(ind+1)=delta(ind);
    end
end

[delta_aux]=map_eye_data_to_aux(delta,iframe_times,length(aux_data));

results=struct;
results.var1={};
results.stim={};
results.velM={};
results.var2={};
results.runP={};
results.frame_times_arr={};
cnt=1;
cnt2=1;
for tnd=1:length(order)
    counter=(tnd*2)-1;
    if order(tnd)==1
        [var1_arr,var2_arr,fb_stim,fb_velM,frame_times_arr]=fb_calc(stim_id,velM_ind,ids,1500,500,shutterOpen,counter,delta_aux,velM_raw);
        results.var1{tnd}=var1_arr;
        results.stim{tnd}=fb_stim;
        results.velM{tnd}=fb_velM;
        results.var2{tnd}=var2_arr;
        results.runP{tnd}=[];
        results.frame_times_arr{tnd}=frame_times_arr;
        cnt=cnt+1;
    elseif order(tnd)==2
        [var1_arr,var2_arr,pb_stim,pb_runP,pb_velM,frame_times_arr]=pb_calc(stim_id,velM_ind,velP_ind,ids,1500,1000,shutterOpen,counter,delta_aux,velM_raw);
        results.var1{tnd}=var1_arr;
        results.stim{tnd}=pb_stim;
        results.velM{tnd}=pb_velM;
        results.var2{tnd}=var2_arr;
        results.runP{tnd}=pb_runP;
        results.frame_times_arr{tnd}=frame_times_arr;
        cnt2=cnt2+1;
    end
end

var1_fb={};
var1_pb={};
var2_fb={};
var2_pb={};
for cnd=1:length(ids)
    cnt=0;
    cnt2=0;
    cnt1=0;
    cnt3=0;
    for mnd=1:length(results.var1)
        if order(mnd)==1
            if ~isempty(results.var1{mnd}{cnd})
                var1_fb{cnd}(:,cnt+1:cnt+size(results.var1{mnd}{cnd},2))=results.var1{mnd}{cnd};
                cnt=cnt+size(results.var1{mnd}{cnd},2);
            else
                var1_fb{cnd}=[];
            end
            if ~isempty(results.frame_times_arr{mnd}{cnd})
                var2_fb{cnd}(:,cnt1+1:cnt1+size(results.frame_times_arr{mnd}{cnd},2))=results.frame_times_arr{mnd}{cnd};
                cnt1=cnt1+size(results.frame_times_arr{mnd}{cnd},2);
            else
                var2_fb{cnd}=[];
            end
        elseif order(mnd)==2
            if ~isempty(results.var1{mnd}{cnd})
                var1_pb{cnd}(:,cnt2+1:cnt2+size(results.var1{mnd}{cnd},2))=results.var1{mnd}{cnd};
                cnt2=cnt2+size(results.var1{mnd}{cnd},2);
            else
                var1_pb{cnd}=[];
            end
            if ~isempty(results.frame_times_arr{mnd}{cnd})
                var2_pb{cnd}(:,cnt3+1:cnt3+size(results.frame_times_arr{mnd}{cnd},2))=results.frame_times_arr{mnd}{cnd};
                cnt3=cnt3+size(results.var2{mnd}{cnd},2);
            else
                var2_pb{cnd}=[];
            end
        end
    end
end

eye_fb_frames=var2_fb;
eye_pb_frames=var2_pb;
eye_delta_fb=var1_fb;
eye_delta_pb=var1_pb;
% indi=1:length(iframe_times);
% for bnd=1:size(var1_fb,2)
%     if ~isempty(var1_fb{bnd})
%         for xnd=1:size(var1_fb{bnd},2)
%             view_eye_stack(idata(:,:,indi(ismember(iframe_times,var2_fb{bnd}(:,xnd)))),imeta_data);
%         end
%     end
% end

if draw_fig
    prev_max=1;
    for knd=1:length(var1_fb)
        cur_max=max([max(var1_fb{knd}) max(var1_pb{knd}) prev_max]);
        prev_max=cur_max;
    end
    
    sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
    figure;
    for ind=1:length(ids)
        subplot(2,7,sequ(ind))
        rectangle('position',[1500 0.5 1000 cur_max],'facecolor',[0.95 0.95 0.95],'linestyle','none')
        if ~isempty(var1_fb{ind})
            hold on
            plot(var1_fb{ind})
        end
        axis tight
        axis off
        box off
        ylim([0 cur_max])
        xlim([0 4000])
        drawnow
    end
    
    sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
    figure;
    for ind=1:length(ids)
        subplot(2,7,sequ(ind))
        rectangle('position',[1500 0.5 1000 cur_max],'facecolor',[0.95 0.95 0.95],'linestyle','none')
        if ~isempty(var1_pb{ind})
            hold on
            plot(var1_pb{ind})
        end
        axis tight
        axis off
        box off
        ylim([0 cur_max])
        xlim([0 4000])
        drawnow
    end
    
    eye_delta_fb=var1_fb;
    eye_delta_pb=var1_pb;
end

% psonsets=[];
% for und=1:size(eye_fb_frames{11},2)
%     psonsets(und)=eye_fb_frames{2}(1,und);
% end
% 
% ipsonsets=[];
% for ind=1:length(psonsets)
%     [~,tmp]=min(abs(iframe_times-psonsets(ind)));
%     ipsonsets(ind)=tmp;
% end
% 
% for gnd=1:length(ipsonsets)
% view_eye_stack(idata(:,:,ipsonsets(gnd)-10:ipsonsets(gnd)+10),imeta_data)
% end